package reptile

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"net/http"
	"robot/study/LiveRobot/define"
	"robot/study/LiveRobot/sdk/zlog"
	"strconv"
	"strings"
)

//socket链接qq号
func Sendmsg(m *define.Msg) {
	var b define.Body
	var bp define.Bodyprivate
	var url string
	var x []byte
	if m.Msg_type == "group" {
		url = "http://127.0.0.1:5700/send_group_msg"
		b = define.Body{
			Group_id: m.Number,
			Message:  m.Msg,
		}
		x, _ = json.Marshal(b)
	} else {
		url = "http://127.0.0.1:5700/send_private_msg"
		bp = define.Bodyprivate{
			User_id: m.Number,
			Message: m.Msg,
		}
		x, _ = json.Marshal(bp)
	}
	res, err := http.Post(url, "application/json;charset=utf-8", strings.NewReader(string(x)))
	if err != nil {
		zlog.Error("post发送失败", err)
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {

		}
	}(res.Body)
	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		zlog.Error("读取响应失败")
	}
	zlog.Info("发送数据：", string(x))
	zlog.Info("响应数据：", string(content))

}

//获取群成员列表
func Getgroup(id int) {
	var url = "http://127.0.0.1:5700/get_group_member_list?group_id=" + strconv.Itoa(id)
	var gr define.Group
	resp, err := http.Get(url)
	if err != nil {
		zlog.Error("获取失败")
	}

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {

		}
	}(resp.Body)
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		zlog.Error("读取失败")
	}
	err2 := json.Unmarshal(body, &gr)
	if err2 != nil {
		zlog.Error("解析失败:", err)
	}
	zlog.Info(gr.Data)
}

//删除消息
func Detmsg(r string) {
	s := "[CQ:tts,text=" + r + "]"
	mes := &define.Msg{
		Msg_type: "group",
		Number:   861335583,
		Msg:      s,
	}
	Sendmsg(mes)
}
